<script setup lang="jsx">
import { ref, onMounted, nextTick, unref } from 'vue'
import { DraggableModal } from '@/components/core/draggable-modal'
import { getAuthStatus } from '@/api/home'
import stepPitchOn1 from '@/assets/images/step-pitch-on-1.png'
import stepPitchOn2 from '@/assets/images/step-pitch-on-2.png'
import stepPitch1 from '@/assets/images/step-pitch-1.png'
import stepPitch2 from '@/assets/images/step-pitch-2.png'
import { checkAccount, resetPassword } from '@/api/account'
import { message } from 'ant-design-vue'
const props = defineProps({
  insuredInfo: {
    type: Object,
    required: () => {}
  },
  policyInfo: {
    type: Object,
    required: () => {}
  },
  accountInfo: {
    type: Object,
    required: () => {}
  }
})

const emit = defineEmits(['updateVisible'])

const loading = ref(false)
const weatchData = ref(null)
const step = ref(1)
const formData = ref({
  account: props.accountInfo.account
})
const formDataTo = ref({
  userName: props.insuredInfo.memberId,
  forceLogout: true,
  forceUpdatePw: true
})
const formRefTo = ref()
const formRef = ref()
const rules = {
  account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  Ihealth: [{ required: true, message: '请输入ihealth账号', trigger: 'blur' }]
}
const rulesTo = {
  password: [
    {
      required: true,
      message: '请输入新密码',
      trigger: 'blur'
    },
    {
      pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,15}$/,
      message: '必须是8-15位包含大小写字母与数字的混合密码',
      trigger: 'blur'
    }
  ]
}
async function initData(type) {
  const { insuredId } = unref(props.policyInfo)
  let res = null
  const data = { insureId: insuredId }
  try {
    res = await getAuthStatus(data)
  } catch (err) {
    console.log('err', err)
  }
  if (res?.isSuccess) {
    weatchData.value = res?.data || {}
  }
}

const weatchVisible = ref(false)
onMounted(async () => {
  await nextTick()
  weatchVisible.value = true
  initData()
})
function handleCancel() {
  weatchVisible.value = false
  emit('updateVisible', false)
}
async function nextStep() {
  formRef.value
    .validate()
    .then(async () => {
      let res = null
      try {
        res = await checkAccount(formData.value.Ihealth)
      } catch (err) {
        console.log('err', err)
      }
      if (res.isSuccess) {
        if (res.data) {
          step.value = step.value + 1
        } else {
          message.error('ihealth账号校验不通过')
        }
      }
    })
    .catch((error) => {
      console.log('error', error)
    })
}
async function submit() {
  formRefTo.value
    .validate()
    .then(async () => {
      let res = null
      loading.value = true
      try {
        res = await resetPassword({
          ...formDataTo.value,
          usernameType: props.accountInfo.accountType
        })
      } catch (err) {
        console.log('err', err)
        loading.value = false
      }
      if (res.isSuccess) {
        loading.value = false
        message.success('重置成功')
        handleCancel()
      }
    })
    .catch((error) => {
      console.log('error', error)
    })
}
</script>
<template>
  <DraggableModal
    :showCloseIcon="true"
    :footer="null"
    @cancel="handleCancel"
    v-model:open="weatchVisible"
    width="498px"
    title="欣服务重置密码"
  >
    <div class="main-header step1" v-if="step === 1">
      <div class="step-box1">
        <img :src="stepPitchOn1" alt="" />
        <div class="content">
          <span class="serial">1</span>
          <span class="text">用户验证</span>
        </div>
      </div>
      <div class="step-box2">
        <img :src="stepPitch2" alt="" />
        <div class="content">
          <span class="serial">2</span>
          <span class="text">重置密码</span>
        </div>
      </div>
    </div>
    <div class="main-header step2" v-if="step === 2">
      <div class="step-box1">
        <img :src="stepPitch1" alt="" />
        <div class="content">
          <span class="serial">1</span>
          <span class="text">用户验证</span>
        </div>
      </div>
      <div class="step-box2">
        <img :src="stepPitchOn2" alt="" />
        <div class="content">
          <span class="serial">2</span>
          <span class="text">重置密码</span>
        </div>
      </div>
    </div>
    <div class="main-content" v-if="step === 1">
      <a-form ref="formRef" :model="formData" :rules="rules">
        <a-row :gutter="0">
          <a-col :span="20" class="mb-30">
            <a-form-item label="欣服务账号" name="account">
              <a-input v-model:value="formData.account" disabled placeholder="请输入" />
            </a-form-item>
          </a-col>
          <a-col :span="20" class="ihealth">
            <a-form-item label="ihealth账号" name="Ihealth">
              <a-input
                v-model:value="formData.Ihealth"
                :allowClear="true"
                placeholder="请输入您的ihealth核心系统账号"
              />
            </a-form-item>
            <div class="text">您的万欣和ihealth账号核心系统用户名</div>
          </a-col>
        </a-row>
      </a-form>
      <div class="footer">
        <a-button key="back" @click="handleCancel">取消</a-button>
        <a-button type="primary" class="next-step" @click="nextStep">下一步</a-button>
      </div>
    </div>
    <div class="main-content" v-if="step === 2">
      <a-form ref="formRefTo" :model="formDataTo" :rules="rulesTo">
        <a-row :gutter="0">
          <a-col :span="20" class="mb-30">
            <a-form-item label="账号" name="account">
              <a-input v-model:value="formDataTo.userName" disabled placeholder="请输入" />
            </a-form-item>
          </a-col>
          <a-col :span="20" class="ihealth">
            <a-form-item label="新密码" name="password" autocomplete="off">
              <a-input-password v-model:value="formDataTo.password" placeholder="请输入">
              </a-input-password>
            </a-form-item>
            <div class="text-rlue">密码设置规则：请输入8-15位包含大小写字母、数字混合设置密码</div>
          </a-col>
          <a-col :span="20" class="mb-25">
            <a-form-item label="强制登出" name="forceLogout">
              <a-switch v-model:checked="formDataTo.forceLogout" disabled />
            </a-form-item>
          </a-col>
          <a-col :span="20" class="mb-45">
            <a-form-item label="强制更新密码" name="forceUpdatePw">
              <a-switch v-model:checked="formDataTo.forceUpdatePw" disabled />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div class="footer">
        <a-button key="back" @click="handleCancel">取消</a-button>
        <a-button type="primary" class="next-step" @click="nextStep" v-if="step === 1"
          >下一步</a-button
        >
        <a-button
          type="primary"
          class="next-step"
          @click="submit"
          v-if="step === 2"
          :loading="loading"
          >保存</a-button
        >
      </div>
    </div>
  </DraggableModal>
</template>
<style lang="scss">
.main-content {
  .ant-form-item-label {
    width: 119px !important;
    label {
      font-family: PingFang-SC, PingFang-SC;
      font-weight: 500;
      font-size: 14px;
      color: #333333;
    }
  }
  .ihealth .ant-form-item-control-input {
    width: 100%;
    height: 32px;
    border: 1px solid #dfdfdf;
    border-radius: 4px;
  }
  .ihealth .ant-input {
    border-radius: 4px;
    height: 30px;
    padding-left: 12px;
  }
  .ihealth .ant-input-suffix {
    width: 20px;
  }
  .ant-switch.ant-switch-checked {
    background: #729db0;
  }
  .ant-switch.ant-switch-checked:hover:not(.ant-switch-disabled) {
    background: #97b7c5;
  }
}
</style>
<style lang="scss" scoped>
.mb-30 {
  margin-bottom: 30px;
}
.mb-25 {
  margin-bottom: 25px;
}
.mb-45 {
  margin-bottom: 45px;
}
.ant-col-5 {
  flex: 0 0 20%;
  max-width: 20%;
}
.main-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 45px;
  margin-top: 25px;
  .step-box1,
  .step-box2 {
    position: relative;
    height: 41px;
    img {
      width: 100%;
      height: 100%;
    }
  }

  .step-box1 {
    width: 232px;
    .content {
      .serial {
        width: 16px;
        height: 16px;
        border-radius: 50%;
        text-align: center;
        line-height: 16px;
        background: #ffffff;
        font-weight: bold;
        font-size: 12px;
        color: #729db0;
        margin-right: 10px;
      }
      .text {
        font-weight: bold;
        font-size: 16px;
        color: #ffffff;
      }
    }
  }
  .step-box2 {
    width: 217px;
    .serial {
      width: 16px;
      height: 16px;
      border-radius: 50%;
      text-align: center;
      line-height: 16px;
      background: #999999;
      font-weight: bold;
      font-size: 12px;
      color: #ffffff;
      margin-right: 10px;
    }
    .text {
      font-weight: bold;
      font-size: 16px;
      color: #999999;
    }
  }
  &.step2 {
    .step-box1 {
      .content {
        .serial {
          background: #999999;
          color: #ffffff;
        }
        .text {
          color: #999999;
        }
      }
    }
    .step-box2 {
      .serial {
        background: #ffffff;
        color: #729db0;
      }
      .text {
        color: #ffffff;
      }
    }
  }
  .content {
    position: absolute;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    top: 0;
    left: 0;
  }
}
.main-content {
  padding-bottom: 20px;
  .text {
    padding-left: 119px;
    font-weight: 500;
    font-size: 12px;
    color: #aaaaaa;
    margin-top: 10px;
    margin-bottom: 45px;
  }
  .text-rlue {
    padding-left: 119px;
    font-weight: 500;
    font-size: 12px;
    color: #aaaaaa;
    margin-top: 10px;
    margin-bottom: 27px;
  }
  .footer {
    border-top: #f4f4f4 1px solid;
    height: 52px;
    padding-top: 20px;
    display: flex;
    justify-content: end;
    align-items: center;
    .next-step {
      margin-left: 15px;
    }
  }
}
</style>
